// pages/owner/activityDetail/activityDetail.js
const app = getApp()
var util = require("../../../utils/util.js")
Page({

  data: {
    id: null,
    activity: {
    },
    join_user: [
      {
        img: 'https://img2.baidu.com/it/u=366871850,3503701641&fm=26&fmt=auto&gp=0.jpg',
        name: '李忠伟',
        time: '2021.6.12 13:12:00'
      },
      {
        img: 'https://img0.baidu.com/it/u=2398037827,2745950472&fm=26&fmt=auto&gp=0.jpg',
        name: '王思琪',
        time: '2021.6.12 13:12:00'
      },
      {
        img: 'https://img2.baidu.com/it/u=1644138801,4070846382&fm=26&fmt=auto&gp=0.jpg',
        name: '李大钊',
        time: '2021.6.12 13:12:00'
      },
      {
        img: 'https://img1.baidu.com/it/u=2315299441,3291577213&fm=26&fmt=auto&gp=0.jpg',
        name: '郑大风',
        time: '2021.6.12 13:12:00'
      },
      {
        img: 'https://img2.baidu.com/it/u=1644138801,4070846382&fm=26&fmt=auto&gp=0.jpg',
        name: '李林启',
        time: '2021.6.12 13:12:00'
      },
      {
        img: 'https://img2.baidu.com/it/u=366871850,3503701641&fm=26&fmt=auto&gp=0.jpg',
        name: '王仲伟',
        time: '2021.6.12 13:12:00'
      }
    ],
    inputRegisterCode: null,
  },

  // 点击签到按钮，弹出输入弹窗
  onRegister: function(e){
    this.setData({
      showDialog: true
    })
    console.log(this.data.showDialog)
  },

  // 签到
  register: function (e){
    var registerTime = new Date()
    var data = {
      activityId: this.data.activity.activityId,
      userId: null,
      registerCode: this.data.inputRegisterCode,
      time: util.dateFormat('YYYY-mm-dd HH:MM:SS', registerTime)
    }
    app.sendPostRequest('/community/owner/participate/register', data, (res) => {
      // console.log('业委会签到结果', res)
      if(res.code == 200){
        wx.showToast({
          title: '签到成功!',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          showDialog: false
        })
        this.getState()
      }else{
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  // 报名
  join: function (e){
    var enrollTime = new Date()
    var data = {
      activityId: this.data.activity.activityId,
      userId: null,
      time: util.dateFormat('YYYY-mm-dd HH:MM:SS', enrollTime)
    }
    app.sendPostRequest('/community/owner/enroll', data, (res) => {
      // console.log('报名结果', res)
      if(res.code == 200){
        wx.showToast({
          title: '报名成功!',
          icon: 'success',
          duration: 2000
        })
        this.getState()
      }else{
        wx.showToast({
          title: '请勿重复报名！',
          icon: 'none',
          duration: 2000
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  // 获取当前活动的状态：未报名，已报名未签到，已签到
  getState: function(e) {
    var registerTime = new Date()
    var data = {
      activityId: this.data.id,
      userId: null,
      registerCode: -1,
      time: util.dateFormat('YYYY-mm-dd HH:MM:SS', registerTime)
    }
    // 加载报名和签到状态
    app.sendPostRequest('/community/owner/activity/state', data, (res) => {
      // console.log('活动状态', res)
      this.setData({
        state: res
      })
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    // console.log('活动id', this.data.id)
    app.sendGetRequest('/community/owner/activity/'+this.data.id, (res) => {
      this.setData({
        activity: res.data
      })
    }, (err) => {
      console.log(err.errMsg)
    })
    this.getState()
  }
})